<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>基本资料</span>
    </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm" label-width="100px" >
        
        <el-form-item label="账号:" prop="id">
        <el-input  v-model="ruleForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称:" prop="nickname">
        <el-input  v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
        <el-input  v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item>
        <el-button type="primary" @click="changeUser">提交</el-button>
        <el-button @click="resetFrom">重置</el-button>
        </el-form-item>

      </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserInfo',
  data() {
    return {
      ruleForm:{...this.$store.state.userInfo},
      rules:{
        nickname:[
          {required: true, message: '请输入昵称', trigger: 'blur'},
          { pattern:/^\S{1,15}$/, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        email:[{ 
          required: true, message: '请输入邮箱地址', trigger: 'blur' },{
          type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']
          }],
      }
    }
  },
    methods: {
      resetFrom(){
        this.ruleForm={...this.$store.state.userInfo}
      },
       changeUser(){
          this.$refs.ruleForm.validate(async (value)=>{
          if(!value) return
          const {data:res}= await this.$http.put('/my/userinfo',this.ruleForm)
          if (res.code !==0)this.$message.error(res.message)
          this.$message.success(res.message)
          this.$store.dispatch('getUser')
          })
      }
    },
    // watch:{
    //   "$store.state.userInfo":{
    //     deep:true,
    //     handler(newVal,oldVal){
    //       console.log(11);
    //       this.ruleForm = newVal
    //     }
    //   }
    // }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
